﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using huaguwang;

namespace huaguwang
{
    public static class PageWriteLog
    {
        /// <summary>
        /// 统一日志填写
        /// </summary>
        /// <param name="page"></param>
        /// <param name="e">操作类型</param>
        /// <param name="FuncName">模块名称</param>
        /// <param name="id">操作内容ID</param>
        public static void Exp_PageWriteLog(this Page page, Log_Type e, string FuncName, int id)
        {
            page.Exp_PageWriteLog(e, FuncName, id, "");
        }
        public static void Exp_PageWriteLog(this Page page, Log_Type e, string FuncName, int id, string strMSG)
        {
            page.Exp_PageWriteLog(e, FuncName, new List<string>() { id.ToString() }, strMSG);
        }
        /// <summary>
        /// 统一日志填写
        /// </summary>
        /// <param name="page"></param>
        /// <param name="e">操作类型</param>
        /// <param name="FuncName">模块名称</param>
        /// <param name="id">操作内容ID</param>
        public static void Exp_PageWriteLog(this Page page, Log_Type e, string FuncName, List<int> id)
        {
            var listid = id.Select(p => p.ToString()).ToList();
            page.Exp_PageWriteLog(e, FuncName, listid, "");
        }
        /// <summary>
        /// 统一日志填写
        /// </summary>
        /// <param name="page"></param>
        /// <param name="e">操作类型</param>
        /// <param name="FuncName">模块名称</param>
        /// <param name="listid">操作内容ID</param>
        public static void Exp_PageWriteLog(this Page page, Log_Type e, string FuncName, List<string> listid, string otherstring)
        {
            var model = new M_T_MenterGroup_Log()
            {
                AddDateTime = DateTime.Now,
                IP = page.Request.UserHostAddress,
                ModuleName = FuncName
            };
            var item = page.Exp_GetUser();
            if(item != null)
            {
                model.UserID = item.id;
            }
            else
            {
                return;
            }
            var id = listid.Exp_JoinList(",", p => p.ToString());
            switch(e)
            {
                case Log_Type.delete:
                    model.FunctionName = FuncName + "删除";
                    model.Explan = "删除的资料ID:" + id;
                    break;
                case Log_Type.add:
                    model.FunctionName = FuncName + "添加";
                    model.Explan = "添加的资料ID:" + id;
                    break;
                case Log_Type.login:
                    model.FunctionName = FuncName + "登录";
                    model.Explan = "管理员ID：" + id;
                    break;
                case Log_Type.update:
                    model.FunctionName = FuncName + "编辑";
                    model.Explan = "编辑的资料id:" + id;
                    break;
                case Log_Type.other:
                    model.FunctionName = FuncName + otherstring;
                    model.Explan = otherstring + "id:" + id;
                    break;
                default:
                    model.FunctionName = FuncName + "无类型操作";
                    model.Explan = "无类型操作的id:" + id;
                    break;
            }

            var _dal = new D_T_MenterGroup_Log_Exp();

            _dal.Add(model);

        }
        /// <summary>
        /// 统一日志填写
        /// </summary>
        /// <param name="page"></param>
        /// <param name="e">操作类型</param>
        /// <param name="FuncName">模块名称</param>
        /// <param name="id">操作内容ID</param>
        //    public static void Exp_PageWriteLog(this Page page, Log_Type e, string FuncName, string deleteString)
        //    {
        //        var model = new M_T_MenterGroup_Log()
        //        {
        //            AddDateTime = DateTime.Now,
        //            IP = page.Request.UserHostAddress,
        //            ModuleName = FuncName
        //        };
        //        var item = page.Exp_GetUser();
        //        if(item != null)
        //        {
        //            model.UserID = item.id;
        //        }
        //        else
        //        {
        //            return;
        //        }
        //        switch(e)
        //        {
        //            case Log_Type.delete:
        //                model.FunctionName = FuncName + "删除";
        //                model.Explan = "删除的资料id:" + deleteString;
        //                break;
        //            case Log_Type.add:
        //                model.FunctionName = FuncName + "添加";
        //                model.Explan = "添加的资料id:" + deleteString;
        //                break;
        //            default:
        //                model.FunctionName = FuncName + "编辑";
        //                model.Explan = "编辑的资料id:" + deleteString;
        //                break;
        //        }

        //        var _dal = new D_T_MenterGroup_Log_Exp();

        //        _dal.Add(model);

        //    }
    }
    /// <summary>
    /// 写日志的类型
    /// </summary>
    public enum Log_Type
    {
        add,
        delete,
        login,
        update,
        other
    }
}
